package com.vf.admin.client.controller;

import com.vf.admin.client.common.ClientBaseController;
import com.vf.admin.client.common.ClientConstants;
import com.vf.admin.client.common.RedisKeys;
import com.vf.admin.client.dao.AppSetupDao;
import com.vf.admin.client.dto.QianbaoDto;
import com.vf.admin.client.service.AppMingXiZuanShiService;
import com.vf.admin.client.service.AppMingxiDuihuanService;
import com.vf.admin.client.service.AppMingxiMeilizhiService;
import com.vf.admin.client.service.AppUserService;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.miaodong.entity.AppSetup;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.utils.BigDecimalUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;

import static com.vf.admin.utils.BigDecimalUtil.mul;

/**
 * 钱包  --黑哲
 */
@Controller
@RequestMapping("/app/wallet")
public class AppWalletController extends ClientBaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    AppUserService appUserService;
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    AppSetupDao appSetupDao;
    @Autowired
    AppMingxiMeilizhiService appMingxiMeilizhiService;
    @Autowired
    AppMingXiZuanShiService appMingXiZuanShiService;
    @Autowired
    AppMingxiDuihuanService appMingxiDuihuanService;

    /**
     * 获取钱包
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Result list() {
        try {

            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            AppUser user = appUserService.get(userId);

            if (user == null) {
                return errorJSON("用户不存在");
            }
            QianbaoDto qianbaoDto = new QianbaoDto();
            qianbaoDto.setJinbi(user.getJinbi());
            qianbaoDto.setZuanshi(user.getDiamonds());
            qianbaoDto.setMeili(user.getCharmvalue());
            //获取设置参数
            AppSetup appSetup = appSetupDao.get();
            //获取提现比例
            double cash = appSetup.getWithdrawalratio();
            //充值比例
            double czBl = appSetup.getRechargeratio();
            //用户自己的魅力值
            long meili = user.getCharmvalue();
            //转换的现金
            double ptCc = BigDecimalUtil.cashForCharmValueConversion(meili, cash, czBl);
            qianbaoDto.setXianjin(ptCc);
            double money=mul(ptCc,czBl);
            qianbaoDto.setDuihuan((int)money);
            qianbaoDto.setFreetime(user.getFreetime());
            return sucessJSON(qianbaoDto);
        }catch (Exception e){
            logger.error("获取钱包异常",e);
            return this.errorJSON("系统繁忙");
        }
    }
}
